<?php
namespace backend\controllers;

use Yii;
use backend\models\Post;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;

use yii\filters\VerbFilter;
use yii\filters\AccessControl;


use yii\data\Pagination;
use backend\base\BaseBackendController;
use base\YiiMobo;

/**
 * Site controller
 */
class UploadController extends BaseBackendController
{
	public $layout = false;
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout', 'signup'],
                'rules' => [
                    [
                        'actions' => ['signup'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                    [
                        'actions' => ['logout', 'index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }



public function actionUpload(){
      $model = new  User();  user 为用户表model；
    
      if ($model->load(Yii::$app->request->post()) && $model->validate()) {
             $upload = $this->uploadedFile($model, 'image');
             $uploadpath = $this->fileExists('./images/' . date('Ymd') . '/');  上传路径
     
              if($model->save()){
                   $upload->saveAs($uploadpath . $upload->name);
               }
      }
}

 public function uploadedFile($model, $item)
    {
        $upload = UploadedFile::getInstance($model, $item);
        $model->image = $upload->name;

        return $upload;
    }

 public function fileExists($uploadpath)
    {

        if(!file_exists($uploadpath)){
            mkdir($uploadpath);
        }

        return $uploadpath;
    }
}
